﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Threading;

namespace DAL
{
    /// <summary>
    /// 数据库基类
    /// Created By : 张占岭
    /// Created Date：2011-10-14
    /// Modify By:
    /// Modify Date:
    /// Modify Reason:
    /// </summary>
    public class TestBase<TEntity> : ContextBase<TEntity> where TEntity : class
    {
        #region Constructors
        public TestBase()
            : this(null)
        {

        }
        public TestBase(IUnitOfWork db)
            : base((TestDataContext)db ?? new TestDataContext(Conn))
        { }
        #endregion

        #region Properties
        protected static string Conn
        {
            get
            {
                if (System.Configuration.ConfigurationManager.ConnectionStrings["backgroundConnectionString"] == null)
                    throw new ArgumentException("请配置SQL连接串");
                return System.Configuration.ConfigurationManager.ConnectionStrings["backgroundConnectionString"].ToString();
            }
        }
        #endregion

        #region Protected Methods

        /// <summary>
        /// 通用更新方法
        /// 子类可以根据自己的逻辑，去复写
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="param">参数列表，奇数为字段，偶数为他前一奇数对应的值，索引0为要更新的主键,1为主键值</param>
        protected virtual void Update(string tableName, params object[] param)
        {
            StringBuilder sbSql = new StringBuilder();
            List<object> listParasm = new List<object>();
            listParasm.Add(param[0]);
            listParasm.Add(param[1]);
            int index = listParasm.Count;
            sbSql.AppendFormat("UPDATE [{0}] SET ", tableName);

            if (index == 0)
            {
                return;
            }
            else
            {
                for (int i = 2; i < param.Count(); i += 2)
                {
                    if (param[index + 1].GetType() == typeof(string))
                        sbSql.AppendFormat("{{{0}}} = '{{{1}}}',", index, index + 1);
                    else
                        sbSql.AppendFormat("{{{0}}} = {{{1}}},", index, index + 1);
                    listParasm.Add(param[index]);
                    listParasm.Add(param[index + 1]);
                    index++;
                    index++;
                }

                sbSql.Remove(sbSql.Length - 1, 1);//移除最后一个逗号
            }

            sbSql.Append(" WHERE [{0}] = {1}");
            _db.ExecuteCommand(sbSql.ToString(), listParasm.ToArray());
        }

        /// <summary>
        ///  计数更新
        ///  子类可以根据自己的逻辑，去复写
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="param">参数列表，索引0为主键名，1表主键值，2为要计数的字段，3为增量</param>
        protected virtual void UpdateForCount(string tableName, params object[] param)
        {
            string sql = "update [{0}] set [{3}]=ISNULL([{3}],0)+{4} where [{1}]={2}";
            List<object> listParasm = new List<object>
            {
                tableName,
                param[0],
                param[1],
                param[2],
                param[3],
            };
            _db.ExecuteCommand(string.Format(sql, listParasm.ToArray()));
        }
        #endregion
    }
}
